package com.wyl.auth.service.impl;
import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.write.metadata.WriteSheet;
import com.wyl.auth.exception.BusinessException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2025/3/28 22:05
 */
@Service
public class ExportService {

    /**
     * 初始化web导出响应
     * @param response http响应
     * @param fileName 文件名称
     */
    public void initResponse(HttpServletResponse response, String fileName) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename*=utf-8''" + fileName + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("导出异常");
        }
    }

    /**
     * web导出
     * @param list                集合列表
     * @param response            http响应
     * @param includeColumnFields 包含字段
     * @param excludeColumnFields 排除字段
     * @param <T>                 泛型
     */
    public <T> void exportExcel(List<T> list, Class<T> clazz, HttpServletResponse response, List<String> includeColumnFields, List<String> excludeColumnFields) {
        try {
            ExcelWriter build = FastExcel.write(response.getOutputStream()).head(clazz)
                    .includeColumnFieldNames(includeColumnFields).excludeColumnFieldNames(excludeColumnFields).build();
            build.write(list, new WriteSheet()).finish();
        } catch (IOException e) {
            throw new BusinessException("导出异常");
        }
    }

    /**
     * web导出
     * @param list                集合列表
     * @param response            http响应
     * @param includeColumnFields 包含字段
     * @param <T>                 泛型
     */
    public <T> void exportExcel(List<T> list, Class<T> clazz, HttpServletResponse response, List<String> includeColumnFields) {
        try {
            ExcelWriter build = FastExcel.write(response.getOutputStream()).head(clazz)
                    .includeColumnFieldNames(includeColumnFields).build();
            build.write(list, new WriteSheet()).finish();
        } catch (IOException e) {
            throw new BusinessException("导出异常");
        }
    }

    /**
     * web导出
     * @param list     集合列表
     * @param response http响应
     * @param <T>      泛型
     */
    public <T> void exportExcel(List<T> list, Class<T> clazz, HttpServletResponse response) {
        try {
            ExcelWriter build = FastExcel.write(response.getOutputStream()).head(clazz).build();
            build.write(list, new WriteSheet()).finish();
        } catch (IOException e) {
            throw new BusinessException("导出异常");
        }
    }

    /**
     * 本地导出
     * @param list 集合列表
     * @param <T>  泛型
     */
    public <T> void exportLocalExcel(List<T> list, Class<T> clazz, String fileName) {
        ExcelWriter build = FastExcel.write(fileName).head(clazz).build();
        build.write(list, new WriteSheet()).finish();
    }

}